package com.myCompany.edu.realtime.app.dws;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.myCompany.edu.realtime.app.BaseAppV1;
import com.myCompany.edu.realtime.bean.CourseChapterUserVideoPlayBean;
import com.myCompany.edu.realtime.common.Constant;
import com.myCompany.edu.realtime.util.AtguiguUtil;
import com.myCompany.edu.realtime.util.DimUtil;
import com.myCompany.edu.realtime.util.DruidDSUtil;
import com.myCompany.edu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class Dws04_DwsCourseChapterUserVideoPlayWindow extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws04_DwsCourseChapterUserVideoPlayWindow().init(
                3004,
                2,
                "Dws04_DwsCourseChapterUserVideoPlayWindow",
                Constant.TOPIC_DWD_TRAFFIC_APPVIDEO
        );
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //1、从kafka播放日志主题读取数据,封装成bean
        SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> beanStream = parseToBean(stream);
        //2、补充chapter_id维度信息，为后续的keuBy开窗聚合做准备
        beanStream = addDim(beanStream);

        //3、按照user_id 和chapter_id分组，开窗聚合
        SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> resultStreamWithoutDim = WindowAndAgg(beanStream);

        //4、对结果添加维度
        SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> resultStream = addChapterName(resultStreamWithoutDim);

        //5、写出到clickhouse
        writeToClickHouse(resultStream);


    }

    private void writeToClickHouse(SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> resultStream) {
        resultStream.addSink(FlinkSinkUtil.getClickHouseSink("dws_course_chapter_user_video_play_window",CourseChapterUserVideoPlayBean.class));
    }

    private SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> addChapterName(SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> resultStreamWithoutDim) {
        return resultStreamWithoutDim
                .map(new RichMapFunction<CourseChapterUserVideoPlayBean, CourseChapterUserVideoPlayBean>() {

                    private DruidPooledConnection conn;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        DruidDataSource dataSource = DruidDSUtil.getDataSource();
                        conn = dataSource.getConnection();
                    }

                    @Override
                    public void close() throws Exception {
                       if (conn != null){
                           conn.close();
                       }
                    }

                    @Override
                    public CourseChapterUserVideoPlayBean map(CourseChapterUserVideoPlayBean bean) throws Exception {
                        JSONObject dimChapterInfo = DimUtil.readDimFromPhoenix(conn, "dim_chapter_info", bean.getChapterId());
                        bean.setChapterName(dimChapterInfo.getString("CHAPTER_NAME"));
                        return bean;
                    }
                });
    }

    private SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> WindowAndAgg(SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> beanStream) {
        return beanStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<CourseChapterUserVideoPlayBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((bean,ts) -> bean.getTs())
                )
                .keyBy(bean -> bean.getUserId() + "_" + bean.getChapterId())
                .window(TumblingEventTimeWindows.of(Time.minutes(1)))
                .reduce(new ReduceFunction<CourseChapterUserVideoPlayBean>() {
                            @Override
                            public CourseChapterUserVideoPlayBean reduce(CourseChapterUserVideoPlayBean bean1,
                                                                         CourseChapterUserVideoPlayBean bean2) throws Exception {
                                bean1.setPlayCt(bean1.getPlayCt() + bean2.getPlayCt());
                                bean1.setPlaySec(bean1.getPlaySec() + bean2.getPlaySec());
                                return bean1;
                            }
                        },
                        new ProcessWindowFunction<CourseChapterUserVideoPlayBean, CourseChapterUserVideoPlayBean, String, TimeWindow>() {
                            @Override
                            public void process(String key,
                                                Context ctx,
                                                Iterable<CourseChapterUserVideoPlayBean> iterable,
                                                Collector<CourseChapterUserVideoPlayBean> collector) throws Exception {
                                CourseChapterUserVideoPlayBean bean = iterable.iterator().next();
                                bean.setStt(AtguiguUtil.toDateTime(ctx.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(ctx.window().getEnd()));
                                bean.setTs(System.currentTimeMillis());
                                collector.collect(bean);
                            }
                        });
    }

    private SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> addDim(SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> beanStream) {
        return beanStream
                .map(new RichMapFunction<CourseChapterUserVideoPlayBean, CourseChapterUserVideoPlayBean>() {

                    private DruidPooledConnection conn;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        DruidDataSource dataSource = DruidDSUtil.getDataSource();
                        conn = dataSource.getConnection();
                    }

                    @Override
                    public void close() throws Exception {
                        if(conn != null){
                            conn.close();
                        }
                    }

                    @Override
                    public CourseChapterUserVideoPlayBean map(CourseChapterUserVideoPlayBean bean) throws Exception {
                        //从video_info中补充chapter_id
                        JSONObject dimVideoInfo = DimUtil.readDimFromPhoenix(conn, "dim_video_info", bean.getVideo_id());
                        bean.setChapterId(dimVideoInfo.getString("CHAPTER_ID"));
                        return bean;
                    }
                });
    }

    private SingleOutputStreamOperator<CourseChapterUserVideoPlayBean> parseToBean(DataStreamSource<String> stream) {

        //一个视频对应一个章节，但是一个章节会对应多个视频
        return stream
                .map(JSON::parseObject)
                .keyBy(obj -> obj.getString("uid") + "_" + obj.getString("video_id") )
                .map(new MapFunction<JSONObject, CourseChapterUserVideoPlayBean>() {
                    @Override
                    public CourseChapterUserVideoPlayBean map(JSONObject object) throws Exception {
                        String userId = object.getString("uid");
                        String videoId = object.getString("video_id");
                        Long playSec = object.getLong("play_sec");
                        Long playCt = 0L;
                        if(playSec<30){
                            playCt = 1L;
                        }
                        Long ts = object.getLong("ts");
                        return CourseChapterUserVideoPlayBean.builder()
                                .stt("")
                                .edt("")
                                .userId(userId)
                                .video_id(videoId)
                                .playSec(playSec)
                                .playCt(playCt)
                                .ts(ts)
                                .build();
                    }
                });

    }
}
